var myfields = [
         { name: "id", 	   type: "int"},
         { name: "name",   type: "string"},
         { name: "surname", type: "string"} //esli ubrat propadaet surname u develop
];

var cm = new Ext.grid.ColumnModel({
        columns:[{ header: "id", dataIndex: "id", width: 40},
                 { header: "name", dataIndex: "name", width: 60, locked: false },
                 { header: "surname", dataIndex: "surname", width: 60}],
        defaults: {
    		sortable: true,
    		//menuDisabled: false,
    		width: 50
        }
});

var getJsonProxy = function(id){
    var proxy = new Ext.data.HttpProxy({
        method: 'GET',
        prettyUrls: false,
        url: 'application/getGrid?'+id,
        api: {
            create  : 'application/create',
            update  : 'application/update'
        }
    });
    return proxy;
};

var getJsonWriter = function(){
    var writer = new Ext.data.JsonWriter({
        encode: true,
        writeAllFields: true
    });
	return writer
}

var getJsonReader = function(){
     var reader = new Ext.data.JsonReader({
        idProperty: 'id',
        fields: myfields
     });
     return reader;
};	

var getStore  = function(List, id){
    //alert(id);
    console.log('it is getStore ' + id);
    var reader = this.getJsonReader(id);
    var proxy = this.getJsonProxy(id);
    var writer = this.getJsonWriter();
    var store = new Ext.data.Store({
       id: 'store',
       reader: reader,
       writer: writer,
       //proxy: proxy,
       url: 'application/getGrid?'+id,
       data: List,
       autoLoad: true
       //remoteSort: true
    });   
    return store;
};

var getnGrid = function(List, id)
{     
    //alert(id);
    console.log('it is getnGrid ' + id);
	var grid;
    var st = this.getStore(List, id);
    console.info(st.data.items);
    
    var ct = Ext.getCmp('GridID').body;
    var pluginFormId = Ext.id();
    ct.update(String.format('<div id="{0}"/>', pluginFormId));
    
    //if (!Ext.getCmp('GridID'))
    {
        grid = new Ext.grid.GridPanel({
            id: 'GridID',
            renderTo:pluginFormId,
            autoscroll:  true,
            autoHeight: true,
            autoWidth: true,
            store: st,
            cm: cm//,

        });
    }
    Ext.reg('GridID', grid);
    return grid;
}


var getGridItems = function(id){
    var gid;
    if(!id){
        node = Ext.getCmp('TreeID').getRootNode();
        if (node){
            id = node.id
            gid = getTreeItemID(id);
        }
        else
            gid = 'orgid=1';
    }
    else
        gid = getTreeItemID(id);
    
    //alert(gid);
    console.log('getGridItems: ' + id);
    Ext.Ajax.request({
        url: 'application/getGrid?'+gid,
        method: 'GET',
        success: function(response, opts){
            var List =  Ext.decode(response.responseText);
            //alert(response.responseText);
            console.log('request info: ' + response.responseText);
            getnGrid(List, gid);
            
        },
        params: {did: id}

    });
};

Ext.onReady(function(){    
    getGridItems();
});
